package com.cn.lotary.base.web;

import com.cn.lotary.util.CilentInfo;
import com.cn.lotary.util.LoggerUtils;
import com.cn.lotary.util.MessageBundle;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;

/**
 * in testing
 *
 * @author user192
 */
public abstract class LoginInterface {

    @NotNull
    private String name;
    @NotNull
    private String password;

    protected abstract boolean isLogin();

    protected void setSession(String key, Object o) {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        session.setAttribute(key, o);
    }

    public void login() {
        if (name != null && !name.equals("") && password != null && !password.equals("")) {

            if (isLogin()) {
                String page = "./index.xhtml";
                redrect(page);
            } else {
                MessageBundle.showLocalWarning(MessageBundle.ACCOUNT_NAME_AND_PASSWORD_CAN_NOT_BE_EMPTY);
            }
        } else {
            MessageBundle.showLocalWarning(MessageBundle.ACCOUNT_NAME_AND_PASSWORD_CAN_NOT_BE_EMPTY);
        }

    }

    private void redrect(String page) {
        try {
            FacesContext.getCurrentInstance().getExternalContext().redirect(page);
        } catch (Exception ex) {
            LoggerUtils.error(ex);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    protected String getIp() {
        return CilentInfo.getIp();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
